// pages/articledetail/articledetail.js
var util = require('../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: '',
    articleid: '',
    offline: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    wx.getNetworkType({
      success: function (res) {
        // 返回网络类型, 有效值：
        // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
        var networkType = res.networkType
        if (networkType == "none") {
          that.setData({
            offline: true
          })
        }
        else{
          const id = options.id;//文章id
          const accountid = app.globalData.accountid;
          var url = app.globalData.serverBaseAddr + '/apip/a/' + id + '/' + accountid;
          var urlParameter = util.GetSign(null);
          url += "?" + urlParameter
          that.setData({
            url: url,
            articleid: id
          })
        }
      }
    })
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const that = this;
    //记录分享次数
    var shareObj = {
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {
          const accountid = app.globalData.accountid;
          const articleid = that.data.articleid;
          const id = util.NewGuid();
          const url = "/apid/c/133";
          const params = "articleid=" + articleid + "&accountid=" + accountid + "&id=" + id
          util.DoCommand(url, params, null, function (data) { }, null, that)
        }

      },
      fail: function () {
        // 转发失败之后的回调
        if (res.errMsg == 'shareAppMessage:fail cancel') {
          // 用户取消转发
        } else if (res.errMsg == 'shareAppMessage:fail') {
          // 转发失败，其中 detail message 为详细失败信息
        }
      },
      complete: function () {
        // 转发结束之后的回调（转发成不成功都会执行）
      }
    }
    return shareObj;
  },
  bindGetMsg:function(e){
    const that=this;
    const data=e.detail.data;
    //收藏
    var favoriteUrl=""
    var faddUrl="/apid/c/137";//增加收藏
    var fdeleteUrl = "/apid/e/139"//取消收藏
  
    //点赞
    var supportUrl = ""
    var saddUrl = "/apid/c/138";//增加点赞
    var sdeleteUrl = "/apid/e/140";//取消点赞
    for(var i in data){
      var _action = data[i].action;
      var _val = data[i].value;
      if (_action =="favorite"){
        if (_val==1){
          favoriteUrl = faddUrl;
        }
        else{
          favoriteUrl = fdeleteUrl;
        }
      }
      if (_action == "support") {
        if (_val == 1) {
          supportUrl = saddUrl;
        }
        else {
          supportUrl = sdeleteUrl;
        }
      }
    }

    const accountid = app.globalData.accountid;
    const articleid = that.data.articleid;
    const id = util.NewGuid();
    const params = "articleid=" + articleid + "&accountid=" + accountid + "&id=" + id
   
    if (favoriteUrl!=""){
      util.DoCommand(favoriteUrl, params, null, function (data) { }, null, that)
    }
    if (supportUrl != "") {
      util.DoCommand(supportUrl, params, null, function (data) { }, null, that)
    }
  }
})